/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

include( "shared.lua" )
include( "admin_menu.lua" )
include( "inventory_shared.lua" )
require( "datastream" )


INV, ABT = {}		//Might localize these for the store and the bag.
ACCStr = ""			//


local STPANEL = {}

local ToolImage = "VGUI/gmod_tool"
local AbilityImage = "gui/arrow"		//Custome one later
local SENTImage = "VGUI/spawnmenu/sent"		//Custom one later
local FLAGSImage = "sprites/key_14"		//Custom one later


local SelectedStoreTable = StoreTools
local StoreItemImage = ToolImage	//Default to Tools first
local OldSelected = ""
local SelectedStoreItem = ""
local SelectedStoreItemCost = 0
MEnabled = true
DoAddItems = false


function STPANEL:Init()

	//Create controls here
	local SPanel = self
	OldSelected = ""
	
	local TypeBox = vgui.Create( "DComboBox", self )
		TypeBox:SetMultiple( false )
		TypeBox:AddItem( "Tools" )
		TypeBox:AddItem( "Abilities" )
		TypeBox:AddItem( "SENTs" )
		TypeBox:AddItem( "FLAGS" )
		TypeBox:AddItem( "Accessories" )
		function TypeBox:Think()
			if TypeBox:GetSelectedItems()[1] then
			if TypeBox:GetSelectedItems()[1]:GetValue() == "Tools" and OldSelected != "Tools" then
				SelectedStoreTable = StoreTools
				StoreItemImage = ToolImage
				OldSelected = "Tools"
				SPanel:AddItems()
			elseif TypeBox:GetSelectedItems()[1]:GetValue() == "Abilities" and OldSelected != "Abilities" then
				SelectedStoreTable = StoreAbilities
				StoreItemImage = AbilityImage
				OldSelected = "Abilities"
				SPanel:AddItems()
			elseif TypeBox:GetSelectedItems()[1]:GetValue() == "SENTs" and OldSelected != "SENTs" then
				SelectedStoreTable = StoreSENTs
				StoreItemImage = SENTImage
				OldSelected = "SENTs"
				SPanel:AddItems()
			elseif TypeBox:GetSelectedItems()[1]:GetValue() == "FLAGS" and OldSelected != "FLAGS" then
				SelectedStoreTable = StoreFLAGS
				StoreItemImage = FLAGSImage
				OldSelected = "FLAGS"
				SPanel:AddItems()
			elseif TypeBox:GetSelectedItems()[1]:GetValue() == "Accessories" and OldSelected != "Accessories" then
				SelectedStoreTable = StoreAccessories
				StoreItemImage = "ModelPanel"
				OldSelected = "Accessories"
				SPanel:AddItems()
			end
			end
		end
		
	self.TypeBox = TypeBox
	
	local ItemList = vgui.Create( "DPanelList", self )
		ItemList:EnableHorizontal( false )
		ItemList:EnableVerticalScrollbar( true )
		ItemList:SetPadding( 5 )
		ItemList:SetSpacing( 5 )
		function ItemList:Paint()
			draw.RoundedBox( 2, 0, 0, self:GetWide(), self:GetTall(), Color( 75, 75, 75, 255 ) )
		end
	
	self.ItemList = ItemList
	
	local PointsBox = vgui.Create( "DLabel", self )
		PointsBox:SetTextColor( color_black )
		PointsBox:NoClipping( true )
		function PointsBox:Think()
			PointsBox:SetText( "Points:  " .. LocalPlayer():GetNetworkedInt( "points" ) )
		end
		
	self.PointsBox = PointsBox
	
	RunConsoleCommand( "StreamInvContents" )

end

function InvStreamHook( pl, handle, id, encoded, decoded )
	INV = encoded.INVTABLE
	ABT = encoded.ABTABLE
	ACCStr = encoded.AccStr
	DoAddItems = true
end
datastream.Hook( "InvStream", InvStreamHook )

function STPANEL:AddItems()
	if self.ItemList then self.ItemList:Clear() end
	local Parent = self.ItemList		//Ease of use
	
	for k,v in pairs( SelectedStoreTable ) do
		if !string.find( table.concat( INV, "," ), v ) then
			
			local ItemPanel = vgui.Create( "DPanel" )
				ItemPanel:SetSize( Parent:GetWide() - 20, 72 )		//Actually, let's base this on the 64 icon size, 4 from top and bottom
				function ItemPanel:Paint()
					draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 25, 25, 25, 255 ) )
				end
				
			if StoreItemImage != "ModelPanel" then
				local ItemImage = vgui.Create( "DImage", ItemPanel )
					ItemImage:SetImage( StoreItemImage )
					ItemImage:SetSize( 64, 64 )
					ItemImage:SetPos( 4, 4 )
			else
				local ItemImage = vgui.Create( "SpawnIcon", ItemPanel )
					ItemImage:SetModel( StoreAccModels[v], 1 )	// 1 is the number of the skin
					ItemImage:SetIconSize( 64 )
					ItemImage:SetPos( 4, 4 )
			end
			
			local ItemInfoPanel = vgui.Create( "DPanel", ItemPanel )
				ItemInfoPanel:StretchToParent( 72, 4, 65, 4 )
				function ItemInfoPanel:Paint()
					draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 50, 50, 50, 255 ) )
				end
			
			local ItemInfo = vgui.Create( "DLabel", ItemInfoPanel )
				ItemInfo:SetText( StoreItemInfoS[v] )
				ItemInfo:SizeToContents()
				ItemInfo:Center()
				
			local PurchaseBut = vgui.Create( "DButton", ItemPanel )
			local SPanel = self
				PurchaseBut:SetText( "Buy" )
				PurchaseBut:SetSize( 30, 20 )
				PurchaseBut:AlignTop( 5 )
				PurchaseBut:MoveRightOf( ItemInfoPanel, 2 )
				function PurchaseBut:DoClick()
					SelectedStoreItem = v
					SelectedStoreItemCost = StoreItemCostS[v]
					SPanel:ConfirmPurchase()
					SPanel:GetParent():SetMouseInputEnabled( false )
				end
				
			local Cost = vgui.Create( "DLabel", ItemPanel )
				Cost:SetText( "Cost: " .. StoreItemCostS[v] )
				Cost:NoClipping( true )
				Cost:SizeToContents()
				Cost:AlignBottom( 5 )
				Cost:MoveRightOf( ItemInfoPanel, 4 )
				function Cost:Paint()
					surface.SetDrawColor( Color( 80, 80, 80, 255 ) )
					surface.DrawOutlinedRect( -2, -2, self:GetWide() + 4, self:GetTall() + 4 )
				end
			
			self.ItemList:AddItem( ItemPanel )
		end
	end
	if OldSelected == "" then self.TypeBox:SelectByName( "Tools" ) end
end

function STPANEL:ConfirmPurchase()
	
	local ConfirmPanel = vgui.Create( "DFrame" )
		ConfirmPanel:SetTitle( "Confirm Purchase" )
		ConfirmPanel:SetSize( 400, 100 )
		ConfirmPanel:Center()
		ConfirmPanel:ShowCloseButton( false )
		ConfirmPanel:MakePopup()
		ConfirmPanel:MoveToFront()
		
	local ConfirmLabel = vgui.Create( "DLabel", ConfirmPanel )
		ConfirmLabel:SetText( "Are you sure you want to purchase " .. SelectedStoreItem .. " for " .. SelectedStoreItemCost .. " points?" )
		ConfirmLabel:SizeToContents()
		ConfirmLabel:Center()
		
	local ConfirmYes = vgui.Create( "DButton", ConfirmPanel )
	local SPanel = self
		ConfirmYes:SetText( "Yes" )
		ConfirmYes:SetSize( 50, 25 )
		ConfirmYes:Center()
		ConfirmYes:AlignLeft( 10 )
		ConfirmYes:AlignBottom( 5 )
		function ConfirmYes:DoClick()
			ConfirmPanel:Remove()
			SPanel:GetParent():SetMouseInputEnabled( true )
			RunConsoleCommand( "RequestPurchase", SelectedStoreItem, SelectedStoreItemCost )
		end
		
	local ConfirmNo = vgui.Create( "DButton", ConfirmPanel )
		ConfirmNo:SetText( "No" )
		ConfirmNo:SetSize( 50, 25 )
		ConfirmNo:Center()
		ConfirmNo:AlignRight( 10 )
		ConfirmNo:AlignBottom( 5 )
		function ConfirmNo:DoClick()
			ConfirmPanel:Remove()
			SPanel:GetParent():SetMouseInputEnabled( true )
		end
		
end

function ErrMessNotification( ply, command, args )
	MEnabled = false

	local Message = vgui.Create( "DFrame" )
		Message:SetTitle( "Error" )
		Message:SetSize( 300, 150 )
		Message:Center()
		Message:ShowCloseButton( false )
		Message:MakePopup()
		Message:MoveToFront()
		
	local MessText = vgui.Create( "DLabel", Message )
		MessText:SetText( "You do not have enough points to purchase this item." )
		MessText:SizeToContents()
		MessText:Center()
		
	local Closebut = vgui.Create( "DButton", Message )
		Closebut:SetText( "OK" )
		Closebut:SetSize( 60, 30 )
		Closebut:Center()
		Closebut:AlignBottom( 5 )
		Closebut.DoClick = function()
			Message:Remove()
			MEnabled = true
		end	

end
concommand.Add( "ErrPlayerNotification", ErrMessNotification )

function SuccMessNotification( ply, command, args )
	MEnabled = false

	local Message = vgui.Create( "DFrame" )
		Message:SetTitle( "Item Purchased" )
		Message:SetSize( 300, 150 )
		Message:Center()
		Message:ShowCloseButton( false )
		Message:MakePopup()
		Message:MoveToFront()
		
	local MessText = vgui.Create( "DLabel", Message )
		MessText:SetText( SelectedStoreItem .. " was added to your inventory." )
		MessText:SizeToContents()
		MessText:Center()
		
	local Closebut = vgui.Create( "DButton", Message )
		Closebut:SetText( "OK" )
		Closebut:SetSize( 60, 30 )
		Closebut:Center()
		Closebut:AlignBottom( 5 )
		Closebut.DoClick = function()
			Message:Remove()
			MEnabled = true
		end	

end
concommand.Add( "SuccPlayerNotification", SuccMessNotification )

function STPANEL:PerformLayout()
	
	// Let's make sure we perform the layout of the class this panel is based on here as well.
	self.BaseClass.PerformLayout( self )
	
	self.TypeBox:StretchToParent( 2, 2, self:GetWide() * 0.8, self:GetTall() * 0.2 )
	
	local TX, TY = self.TypeBox:GetPos()
	self.PointsBox:SetWide( 85 )
	self.PointsBox:AlignLeft( 8 )
	self.PointsBox:MoveBelow( self.TypeBox, 10 )
	
	self.ItemList:StretchToParent( self:GetWide() * 0.205, 2, 2, 2 )

end

function STPANEL:Paint()
	//Do Painting here
	
	self.BaseClass.Paint( self )
	
	function self.PointsBox:Paint()
		surface.SetDrawColor( Color( 255, 255, 255, 255 ) )
		surface.DrawRect( -5, -2, self:GetWide(), self:GetTall() + 2 )
		
		surface.SetDrawColor( Color( 0, 0, 0, 255 ) )
		surface.DrawOutlinedRect( -5, -2, self:GetWide(), self:GetTall() + 2 )
	end

end

function STPANEL:PaintOver()		//Might have to add a retrieving items status message to this panel sooner or later

	//Paint over self and child elements here.

end

function STPANEL:Think()
	//Think stuff here
	
	if !MEnabled then
		self:GetParent():SetMouseInputEnabled( false )
		self:SetMouseInputEnabled( false )
	else
		self:GetParent():SetMouseInputEnabled( true )
		self:SetMouseInputEnabled( true )
	end
	
	if DoAddItems then
		self:AddItems()
		DoAddItems = false
	end

end

function STPANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function STPANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function STPANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function STPANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel

end

function STPANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function STPANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function STPANEL:ActionSignal()

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function STPANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function STPANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "InvStore", STPANEL, "DPanel" )